package com.yiwenup.leetcode.site;

import com.yiwenup.leetcode.ListNode;

/**
 * https://leetcode-cn.com/problems/add-two-numbers/
 */
public class No002 {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode res = new ListNode(0);
        ListNode cur = res;
        int tmp = 0;
        while (l1 != null || l2 != null) {
            int val1 = l1 == null ? 0 : l1.val;
            int val2 = l2 == null ? 0 : l2.val;
            int sum = val1 + val2 + tmp;

            tmp = sum / 10;
            ListNode node = new ListNode(sum % 10);
            cur.next = node;
            cur = node;

            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }

        return res.next;
    }
}
